package com.dayu.recovery.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dayu.common.utils.DateUtils;
import com.dayu.common.utils.SecurityUtils;
import com.dayu.recovery.domain.RecoveryGoods;
import com.dayu.recovery.domain.StockCheckHandleLog;
import com.dayu.recovery.mapper.RecoveryGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dayu.recovery.mapper.StockCheckLogMapper;
import com.dayu.recovery.domain.StockCheckLog;
import com.dayu.recovery.service.IStockCheckLogService;
import org.springframework.util.ObjectUtils;


/**
 * 盘库日志Service业务层处理
 * 
 * @author HK
 * @date 2022-07-01
 */
@Service
public class StockCheckLogServiceImpl implements IStockCheckLogService 
{
    @Autowired
    private StockCheckLogMapper stockCheckLogMapper;
    @Autowired
    private RecoveryGoodsMapper recoveryGoodsMapper;


    /**
     * 查询盘库日志
     * 
     * @param id 盘库日志ID
     * @return 盘库日志
     */
    @Override
    public StockCheckLog selectStockCheckLogById(Long id)
    {
        return stockCheckLogMapper.selectStockCheckLogById(id);
    }

    /**
     * 查询盘库日志列表
     * 
     * @param stockCheckLog 盘库日志
     * @return 盘库日志
     */
    @Override
    public List<StockCheckLog> selectStockCheckLogList(StockCheckLog stockCheckLog)
    {
        return stockCheckLogMapper.selectStockCheckLogList(stockCheckLog);
    }

    /**
     * 新增盘库日志
     * 
     * @param stockCheckLog 盘库日志
     * @return 结果
     */
    @Override
    public int insertStockCheckLog(StockCheckLog stockCheckLog)
    {
        stockCheckLog.setCreateId(SecurityUtils.getLoginUser().getUser().getUserId());
        stockCheckLog.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        stockCheckLog.setCreateTime(DateUtils.getNowDate());
        return stockCheckLogMapper.insertStockCheckLog(stockCheckLog);
    }

    /**
     * 修改盘库日志
     * 
     * @param stockCheckLog 盘库日志
     * @return 结果
     */
    @Override
    public int updateStockCheckLog(StockCheckLog stockCheckLog)
    {
        return stockCheckLogMapper.updateStockCheckLog(stockCheckLog);
    }

    /**
     * 批量删除盘库日志
     * 
     * @param ids 需要删除的盘库日志ID
     * @return 结果
     */
    @Override
    public int deleteStockCheckLogByIds(Long[] ids)
    {
        return stockCheckLogMapper.deleteStockCheckLogByIds(ids);
    }

    @Override
    public List<RecoveryGoods> exportGoodsList(Long checkId) {
        if(ObjectUtils.isEmpty(checkId)) {
            throw new RuntimeException("数据错误!");
        }
        StockCheckLog log = selectStockCheckLogById(checkId);
        if(ObjectUtils.isEmpty(log)) {
            throw new RuntimeException("数据错误!");
        }
        RecoveryGoods goods = new RecoveryGoods();
        Map<String, Object> map = new HashMap<>();
        map.put("goodsIds", log.getGoodsIds().split(","));
        goods.setParams(map);
        return recoveryGoodsMapper.selectRecoveryGoodsList(goods);
    }

    /**
     * 删除盘库日志信息
     * 
     * @param id 盘库日志ID
     * @return 结果
     */
    @Override
    public int deleteStockCheckLogById(Long id)
    {
        return stockCheckLogMapper.deleteStockCheckLogById(id);
    }

    @Override
    public List<StockCheckHandleLog> queryCheckHandleList(StockCheckHandleLog bo) {
        return stockCheckLogMapper.queryPageList(bo);
    }

    @Override
    public int addHandleCheckLog(StockCheckHandleLog bo) {
        if(ObjectUtils.isEmpty(bo.getCheckLogId()))
            throw new RuntimeException("盘库日志不能为空");
        if(ObjectUtils.isEmpty(bo.getGoodsId()))
            throw new RuntimeException("商品编码不能为空");
        bo.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        bo.setCreateTime(new Date());
        return stockCheckLogMapper.addEntity(bo);
    }
}
